#include "debug.h"
#include "configfile.h"
#include "tinyxml.h"
#include <string>
#include <stdio.h>
#include <boost/lexical_cast.hpp>
using namespace std;

ConfigFile::ConfigFile(const string &fileName)
{
	m_FileName.assign(fileName);
}

ConfigFile::~ConfigFile()
{

}

void ConfigFile::Init()
{
	DBGPRINT("Info--Get in ConfigFile::Init()\n");
	FILE *fpConfig = fopen(m_FileName.c_str(), "r");
	if (fpConfig==0)
	{
		DBGPRINT ("Error--In ConfigFile::Init()--Cannot open file\n");
		return ;
	}
	
	TiXmlDocument *configDoc = new TiXmlDocument();
	configDoc->LoadFile(m_FileName.c_str());
	TiXmlElement *rootElement = configDoc->RootElement();
	TiXmlElement *attrElement = rootElement->FirstChildElement();
	while (attrElement)
	{
		TiXmlAttribute *attr = attrElement->FirstAttribute();
		if (attr)
		{
			DBGPRINT("Info--Get [%s]=%s\n", attr->Name(), attr->Value());
			m_Content.insert(make_pair(attr->Name(), attr->Value()));
		}
		attrElement = attrElement->NextSiblingElement();
	}
	if (configDoc)
	{
		delete configDoc;
		configDoc = 0;
	}
	DBGPRINT("Info--Get out of ConfigFile::Init()\n");
}

string ConfigFile::ReadString(const string &name)
{
	return m_Content[name];
}

int ConfigFile::ReadInt(const string &name)
{
	return boost::lexical_cast<int>(m_Content[name]);
}


